package com.mdlib.helper.ui.fragments.rush.my;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ljlib.core.base.BaseFragment;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.model.entity.OrderEntity;
import com.mdlib.helper.ui.UIHelper;
import com.mdlib.helper.ui.adapter.RushMyOrderAdapter;
import com.mdlib.helper.ui.widget.springview.DefaultFooter;
import com.mdlib.helper.ui.widget.springview.HelperHeads;
import com.mdlib.helper.ui.widget.springview.SpringView;
import com.mdlib.helper.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;


/**
 * 作者：HQY on 16/11/28 14:25
 * 邮箱：hqy_xz@126.com
 */

public class RushHistoryOrderFragment extends BaseFragment {

    @Bind(R.id.rv_history_rush_order)
    RecyclerView mRvHistoryRushOrder;
    @Bind(R.id.sv_rush_history)
    SpringView mSvRushHistory;

    private List<OrderEntity> orderEntityList = new ArrayList<>();

    private RushMyOrderAdapter mRushMyOrderAdapter;

    private int pageNumber = 1;

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_rush_order_history;
    }


    @Override
    public void onStart() {
        super.onStart();

        orderEntityList.clear();
        pageNumber = 1;
        getHistoryOrder();
    }

    public void init() {


        mRushMyOrderAdapter = new RushMyOrderAdapter(orderEntityList);
        mRvHistoryRushOrder.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvHistoryRushOrder.setAdapter(mRushMyOrderAdapter);
        mRvHistoryRushOrder.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }


            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemClick(adapter, view, position);

                UIHelper.showRushOrderDetails(getActivity(), orderEntityList.get(position).getId() + "");
            }

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemChildClick(adapter, view, position);
            }
        });
        mSvRushHistory.setType(SpringView.Type.FOLLOW);
        mSvRushHistory.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                pageNumber = 1;
                getHistoryOrder();
            }

            @Override
            public void onLoadmore() {
                getHistoryOrder();
            }
        });
        mSvRushHistory.setHeader(new HelperHeads(getActivity()));
        mSvRushHistory.setFooter(new DefaultFooter(getActivity()));
//        getHistoryOrder();
    }

    private void getHistoryOrder() {
        startLoadingDialog(true);
        addSubscribe(DataManager.getInstance().getUserApi()
                .getRushHistoryOrder(pageNumber + "")
                .compose(RxHelper.<ArrayList<OrderEntity>>defaultHandleResult())
                .subscribe(new RxSubscribe<ArrayList<OrderEntity>>() {
                    @Override
                    protected void onSucc(ArrayList<OrderEntity> orderEntities) {
                        stopLoadingDialog();
                        pageNumber++;
                        updateList(orderEntities);
                        mSvRushHistory.onFinishFreshAndLoad();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                        stopLoadingDialog();
                        mSvRushHistory.onFinishFreshAndLoad();

                    }
                }));
    }

    public void updateList(List<OrderEntity> orders) {
        if (orders.size() > 0 && pageNumber >= 2) {
            for (OrderEntity order : orders) {
                orderEntityList.add(order);
            }
            mRushMyOrderAdapter.notifyDataSetChanged();
        } else if (orders.size() <= 0 && pageNumber > 2) {
            pageNumber--;
            ToastUtil.showToast("没有更多数据了!");
        }
    }


}
